package com.jinniulive.platform.module.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jinniulive.platform.R;
import com.jinniulive.platform.bean.LoginInfo;
import com.jinniulive.platform.module.login.fragments.PopupFragment;
import com.jinniulive.platform.urls.NetConfig;

import org.xutils.common.Callback;
import org.xutils.common.util.MD5;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import static com.jinniulive.platform.R.id.activity_person_settings_fl;

public class PersonSettingsActivity extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener {

    private TextView mTextViewName, mTextViewAccount, mTextViewId, mTextViewSex;
    private RelativeLayout mLayoutName, mLayoutHead, mLayoutSex, mLayoutPassword;
    //灰背景
    private ImageButton mImageButton;
    private int userID;
    private String tokenID;
    private String sex;
    private String password;

    private FrameLayout mFrameLayout;
    private FragmentManager mManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_settings);
        initView();
        initData();
        //用户信息 加载
        userInfo();
        setListener();


    }

    private void initData() {
    }

    private void setListener() {
        mLayoutName.setOnClickListener(this);
        mLayoutHead.setOnClickListener(this);
        mLayoutSex.setOnClickListener(this);
        mLayoutPassword.setOnClickListener(this);
        mLayoutHead.setOnFocusChangeListener(this);
    }

    private void userInfo() {
        SharedPreferences sp = getSharedPreferences("autoLogin", MODE_PRIVATE);
        String account = sp.getString("account", "");
        password = sp.getString("password", "");
        String userId = sp.getString("userId", "");
        String userName = sp.getString("userName", "");
        String md5password = MD5.md5(password);
        if (account.isEmpty()||password.isEmpty()){
            try {
                String encode = URLEncoder.encode(userName, "UTF-8");
                String format = String.format(NetConfig.SFDL, userId, encode);
                RequestParams params = new RequestParams(format);
                //设置数据
                setData(params);

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }else {
            String format = String.format(NetConfig.DL, account, md5password);
            RequestParams params = new RequestParams(format);
            //设置数据
            setData(params);
        }
    }

    private void setData(RequestParams params) {
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                LoginInfo loginInfo = gson.fromJson(result, LoginInfo.class);
                LoginInfo.UserslistBean userslistBean = loginInfo.getUserslist().get(0);
                String name = userslistBean.getNickName();

                String loginName = userslistBean.getLoginName();
                sex = userslistBean.getSex();
                userID = userslistBean.getUserID();
                tokenID = userslistBean.getTokenID();
                //赋值
                try {
                    String decodeName = URLDecoder.decode(name, "UTF-8");
                    mTextViewName.setText(decodeName);
                    String decodeSex = URLDecoder.decode(sex, "UTF-8");
                    mTextViewSex.setText(decodeSex);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                mTextViewAccount.setText(loginName);
                mTextViewId.setText(userID + "");

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(PersonSettingsActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView() {
        mTextViewName = (TextView) findViewById(R.id.activity_person_settings_tv_name);
        mTextViewAccount = (TextView) findViewById(R.id.activity_person_settings_tv_account);
        mTextViewId = (TextView) findViewById(R.id.activity_person_settings_tv_id);
        mTextViewSex = (TextView) findViewById(R.id.activity_person_settings_tv_sex);
        mLayoutHead = (RelativeLayout) findViewById(R.id.activity_person_settings_rl_head);
        mLayoutName = (RelativeLayout) findViewById(R.id.activity_person_settings_rl_name);
        mLayoutSex = (RelativeLayout) findViewById(R.id.activity_person_settings_rl_sex);
        mLayoutPassword = (RelativeLayout) findViewById(R.id.activity_person_settings_rl_password);
        mFrameLayout = (FrameLayout) findViewById(R.id.activity_person_settings_fl);
        mImageButton = (ImageButton) findViewById(R.id.activity_person_settings_ib);

        mManager=getSupportFragmentManager();
        FragmentTransaction transaction = mManager.beginTransaction();
        transaction.add(activity_person_settings_fl,new PopupFragment());
        transaction.commit();


    }

    public void onClick(View view) {
        if (view != null) {
            switch (view.getId()) {
                //返回
                case R.id.activity_person_settings_bt_back:
                    finish();
                    break;
                case R.id.activity_person_settings_ib:
                    mImageButton.setVisibility(View.GONE);
                    mFrameLayout.setVisibility(View.GONE);
                    break;
                case R.id.activity_person_settings_rl_head:
                    mImageButton.setVisibility(View.VISIBLE);
                    mFrameLayout.setVisibility(View.VISIBLE);
                    break;
                case R.id.activity_person_settings_rl_name:
                    String name = mTextViewName.getText().toString();
                    Intent intent = new Intent(this, NameSettingActivity.class);
                    intent.putExtra("name", name);
                    if (tokenID!=null && userID !=0){
                        intent.putExtra("tokenID", tokenID);
                        intent.putExtra("userID", userID + "");
                        startActivity(intent);
                    }else {
                        Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
                    }


                    break;
                case R.id.activity_person_settings_rl_sex:
                    Intent intent1 = new Intent(this, SexSettingActivity.class);
                    if (sex != null && tokenID !=null && userID!=0){
                        intent1.putExtra("sex",sex);
                        intent1.putExtra("tokenID", tokenID);
                        intent1.putExtra("userID", userID + "");
                        startActivity(intent1);
                    }else {
                        Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    break;
                case R.id.activity_person_settings_rl_password:
                    if (tokenID!=null && userID!=0 && password!=null ){
                        Intent it=new Intent(this,PasswordSettingActivity.class);
                        it.putExtra("tokenID",tokenID);
                        it.putExtra("userID",userID+"");
                        it.putExtra("password",password);
                        startActivity(it);
                    }else{
                        Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
                    }
                    break;
                default:
                    break;

            }
        }

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        userInfo();
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){

        }else {
            mFrameLayout.setVisibility(View.GONE);
        }
    }
}
